var emailRegExp = new RegExp(
		"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");
function validator(form) {
	validateFieldBlur();
	form.submit(function() {
		return validateFormSubmit();
	});
}
function validateFieldBlur() {
	$(".required").bind('blur', function() {
		validateRequired(this);
	});
	$(".email").bind('blur', function() {
		validateEmail(this);
	});
}

function validateFormSubmit() {
	$("body").mask("请等待...");
	var passcheck = true;
	var check;
	$(".required").each(function() {
		check = validateRequired(this);
		passcheck = passcheck && check;
	});
	if (passcheck) {
		$(".email").each(function() {
			check = validateEmail(this);
			passcheck = passcheck && check;
		});
	}
	if (!passcheck) {
		$("body").unmask();
	}
	return passcheck;
}

function validateRequired(ele) {
	$(ele).removeClass("validation-failed");
	$(ele).nextAll().filter("#requiredTip").remove();
	if ($.trim(ele.value) == "") {
		$(ele).addClass("validation-failed");
		$("<span class='validation-failed' id='requiredTip'>必填</span>")
				.appendTo($(ele).parent());
		return false;
	}
	return true;
}
function validateEmail(email) {
	$(email).removeClass("validation-failed");
	$(email).nextAll().filter("#emailTip").remove();
	if (!emailRegExp.test(email.value) || email.value.indexOf('.') == -1) {
		$(email).addClass("validation-failed");
		$("<span class='validation-failed' id='emailTip'>邮箱格式不对</span>")
				.appendTo($(email).parent());
		return false;
	}
	return true;
}